package com.los.rabbitmq.simple;

import com.los.rabbitmq.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbitmq
 * @description: 消息接收者
 * @author: Yujuan
 * @create: 2018-08-01 21:47
 **/
public class Consumer {
    private static final String QUEUE_NAME="los_simple_queue";
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection= ConnectionUtils.getConnection();

        //创建通道
        Channel channel=connection.createChannel();

        //队列声明(可以不用声明），未保险，还是声明一下
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);

        //声明消费模型
        DefaultConsumer consumer=new DefaultConsumer(channel){
            //获取到达消息
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg=new String(body,"utf-8");
                System.out.println(msg);
            }
        };
        //监听队列
        channel.basicConsume(QUEUE_NAME,true,consumer);
    }
}
